package com.fgs.common.widget.itemView;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fgs.common.R;
import com.fgs.common.utils.CommonUtil;


/**
 * @author fgs
 * @date 2020/3/29
 * @description
 **/
public class ClickItemView extends LinearLayout {
    private LinearLayout mItemLinearLayout;
    private ImageView mLeftImageView;
    private ImageView mRightImageView;
    private TextView mTipTextView;
    private TextView mTitleTextView;
    private TextView mContentTextView;
    private TextView mSubContentTextView;
    private ImageView mArrowImageView;
    private String mTitle;
    private String mContent;
    private String mContentHint;
    private String mSubContent;
    private int mTitleTextColor;
    private int mContentTextColor;
    private int mSubContentTextColor;
    private int mContextHintColor;
    private int mLeftIcon;
    private int mRightIcon;
    private int mLeftIconWidth;
    private int mLeftIconHeight;
    private int mRightIconWidth;
    private int mRightIconHeight;
    private boolean isShowArrow;
    private boolean isShowTittle;
    private boolean isShowTipText;
    private boolean isShowLeftIcon;
    private boolean isShowRightIcon;
    private int mBackgroundId;

    public ClickItemView(Context context) {
        super(context);
        initView(context, null);
    }

    public ClickItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ClickItemView);

        int contentTextGravity;
        try {
            mTitle = typedArray.getString(R.styleable.ClickItemView_ClickItemTitle);
            mContent = typedArray.getString(R.styleable.ClickItemView_ClickItemContent);
            mSubContent = typedArray.getString(R.styleable.ClickItemView_ClickItemSubContent);
            mContentHint = typedArray.getString(R.styleable.ClickItemView_ClickItemContentHint);
            mTitleTextColor = typedArray.getColor(R.styleable.ClickItemView_ClickItemTitleTextColor, context.getResources().getColor(R.color.colorTextBlack));
            mContentTextColor = typedArray.getColor(R.styleable.ClickItemView_ClickItemContentTextColor, context.getResources().getColor(R.color.colorTextPrimary));
            mSubContentTextColor = typedArray.getColor(R.styleable.ClickItemView_ClickItemSubContentTextColor, context.getResources().getColor(R.color.colorTextPrimary));
            mContextHintColor = typedArray.getColor(R.styleable.ClickItemView_ClickItemContentHintTextColor, context.getResources().getColor(R.color.colorTextGray));

            mLeftIcon = typedArray.getResourceId(R.styleable.ClickItemView_ClickItemLeftIcon, 0);
            mRightIcon = typedArray.getResourceId(R.styleable.ClickItemView_ClickItemRightIcon, 0);
            isShowArrow = typedArray.getBoolean(R.styleable.ClickItemView_isShowArrow, true);
            isShowTittle = typedArray.getBoolean(R.styleable.ClickItemView_ClickItemIsTittleShow, true);
            isShowTipText = typedArray.getBoolean(R.styleable.ClickItemView_isShowTip, false);
            isShowLeftIcon = typedArray.getBoolean(R.styleable.ClickItemView_isShowLeftIcon, false);
            isShowRightIcon = typedArray.getBoolean(R.styleable.ClickItemView_isShowRightIcon, false);

            mLeftIconWidth = typedArray.getInteger(R.styleable.ClickItemView_ClickItemLeftIconWidth, 18);
            mLeftIconHeight = typedArray.getInteger(R.styleable.ClickItemView_ClickItemLeftIconHeight, 18);

            mRightIconWidth = typedArray.getInteger(R.styleable.ClickItemView_ClickItemRightIconWidth, 18);
            mRightIconHeight = typedArray.getInteger(R.styleable.ClickItemView_ClickItemRightIconHeight, 18);
            contentTextGravity = typedArray.getInt(R.styleable.ClickItemView_ClickItemContentGravity, 1);
            mBackgroundId = typedArray.getResourceId(R.styleable.ClickItemView_ClickItemBackground, R.drawable.bg_click_item_default);
        } finally {

            typedArray.recycle();
        }


        View view = LayoutInflater.from(context).inflate(R.layout.layout_click_item, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        addView(view);

        mItemLinearLayout = view.findViewById(R.id.layout_click_item);
        mTipTextView = view.findViewById(R.id.layout_click_item_tipTextView);
        mTitleTextView = view.findViewById(R.id.layout_click_item_titleTextView);
        mContentTextView = view.findViewById(R.id.layout_click_item_contentTextView);
        mSubContentTextView = view.findViewById(R.id.layout_click_item_subContentTextView);
        mLeftImageView = view.findViewById(R.id.layout_click_item_leftImageView);
        mRightImageView = view.findViewById(R.id.layout_click_item_rightImageView);
        mArrowImageView = view.findViewById(R.id.layout_v_item_rightArrowImageView);

        setTitle(mTitle);
        setContent(mContent);
        setSubContent(mSubContent);
        setContentHint(mContentHint);
        setTitleTextColor(mTitleTextColor);
        setContentTextColor(mContentTextColor);
        setSubContentTextColor(mSubContentTextColor);
        setContextHintColor(mContextHintColor);
        setShowTitle(isShowTittle);
        setShowArrow(isShowArrow);
        setShowTip(isShowTipText);
        setLeftIcon(mLeftIcon);
        setShowLeftIcon(isShowLeftIcon);
        setRightIcon(mRightIcon);
        setShowRightIcon(isShowRightIcon);

        setLeftIconSize(mLeftIconWidth, mLeftIconHeight);
        setRightIconSize(mRightIconWidth, mRightIconHeight);

        setContentTextGravity(contentTextGravity);
        setItemBackground(mBackgroundId);
    }

    private void setItemBackground(int backgroundId) {
        if (backgroundId != 0) {
            mItemLinearLayout.setBackgroundResource(backgroundId);
        }
    }

    private void setShowTitle(boolean isShowTittle) {
        if (isShowTittle) {
            mTitleTextView.setVisibility(View.VISIBLE);
        } else {
            mTitleTextView.setVisibility(View.GONE);
        }
    }

    private void setContentTextGravity(int gravity) {
        switch (gravity) {
            case 0:
                mContentTextView.setGravity(Gravity.LEFT);
                break;
            case 1:
                mContentTextView.setGravity(Gravity.RIGHT);
                break;
            case 2:
                mContentTextView.setGravity(Gravity.CENTER);
                break;
        }
    }

    public void setItemClickable(boolean isClickable) {
        setClickable(isClickable);
        setEnabled(isClickable);
    }


    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        this.mTitle = title;
        mTitleTextView.setText(title);
    }

    public String getContent() {
        return mContent;
    }

    public void setContent(String content) {
        this.mContent = content;
        mContentTextView.setText(content);
    }

    public void setSubContent(String content) {
        if (!TextUtils.isEmpty(content)) {
            mSubContentTextView.setVisibility(View.VISIBLE);
        } else {
            mSubContentTextView.setVisibility(View.GONE);
        }
        this.mSubContent = content;
        mSubContentTextView.setText(content);
    }

    public String getContentHint() {
        return mContentHint;
    }

    public void setContentHint(String contentHint) {
        this.mContentHint = contentHint;
        if (!TextUtils.isEmpty(contentHint)){

            mContentTextView.setHint(contentHint);
        }
    }

    public int getTitleTextColor() {
        return mTitleTextColor;
    }

    public void setTitleTextColor(int titleTextColor) {
        this.mTitleTextColor = titleTextColor;
        mTitleTextView.setTextColor(titleTextColor);
    }

    public int getContentTextColor() {
        return mContentTextColor;
    }

    public void setContentTextColor(int contentTextColor) {
        this.mContentTextColor = contentTextColor;
        mContentTextView.setTextColor(contentTextColor);
    }

    public void setSubContentTextColor(int textColor) {
        this.mSubContentTextColor = textColor;
        mSubContentTextView.setTextColor(textColor);
    }


    public int getContextHintColor() {
        return mContextHintColor;
    }

    public void setContextHintColor(int contextHintColor) {
        this.mContextHintColor = contextHintColor;
        mContentTextView.setHintTextColor(contextHintColor);
    }

    public int getLeftIcon() {
        return mLeftIcon;
    }

    public void setLeftIcon(int leftIcon) {
        this.mLeftIcon = leftIcon;
        if (leftIcon > 0) {
            mLeftImageView.setBackgroundResource(leftIcon);
        } else {
            mLeftImageView.setBackgroundResource(0);
        }
    }

    public boolean isShowArrow() {
        return isShowArrow;
    }

    public void setShowArrow(boolean showArrow) {
        isShowArrow = showArrow;
        if (showArrow) {
            mArrowImageView.setVisibility(View.VISIBLE);
        } else {
            mArrowImageView.setVisibility(View.GONE);
        }
    }

    public boolean isShowLeftIcon() {
        return isShowLeftIcon;
    }

    public void setShowLeftIcon(boolean showLeftIcon) {
        isShowLeftIcon = showLeftIcon;
        if (showLeftIcon) {
            mLeftImageView.setVisibility(View.VISIBLE);
        } else {
            mLeftImageView.setVisibility(View.GONE);
        }
    }

    public int getRightIcon() {
        return mRightIcon;
    }

    public void setRightIcon(int rightIcon) {
        this.mRightIcon = rightIcon;
        if (rightIcon > 0) {

            mRightImageView.setBackgroundResource(rightIcon);
        } else {

            mRightImageView.setBackgroundResource(0);
        }
    }

    public boolean isShowRightIcon() {
        return isShowRightIcon;
    }


    private void setShowTip(boolean isShowTipText) {

        this.isShowTipText = isShowTipText;
        if (isShowTipText) {
            mTipTextView.setVisibility(View.VISIBLE);
        } else {
            mTipTextView.setVisibility(View.INVISIBLE);
        }
    }

    public void setShowRightIcon(boolean showRightIcon) {
        isShowRightIcon = showRightIcon;
        if (showRightIcon) {
            mRightImageView.setVisibility(View.VISIBLE);
        } else {
            mRightImageView.setVisibility(View.GONE);
        }
    }

    public void setLeftIconSize(int width, int height) {
        LayoutParams layoutParams = (LayoutParams) mLeftImageView.getLayoutParams();
        layoutParams.width = (int) CommonUtil.convertDpToPixel(width);
        layoutParams.height = (int) CommonUtil.convertDpToPixel(height);
        mLeftImageView.setLayoutParams(layoutParams);
    }

    public void setRightIconSize(int width, int height) {
        LayoutParams layoutParams = (LayoutParams) mRightImageView.getLayoutParams();
        layoutParams.width = (int) CommonUtil.convertDpToPixel(width);
        layoutParams.height = (int) CommonUtil.convertDpToPixel(height);
        mRightImageView.setLayoutParams(layoutParams);
    }


}
